/**
 * 
 */
package com.effectivsocial.web.controller;


import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.omnifaces.util.Faces;
import org.springframework.context.annotation.Scope;

import com.effectivsocial.core.i18n.I18nAware;
import com.effectivsocial.domain.Application;
import com.effectivsocial.domain.TwitterApp;
import com.effectivsocial.service.api.ApplicationService;
import com.effectivsocial.service.api.TwitterService;



/**
 * @author Dhrubo
 * 
 */
@Named
@Scope("view")
@Slf4j
public class EditApplicationForm implements Serializable{
	private static final long serialVersionUID = 1L;
	
	@Getter @Setter
	private Application application;
	
	@Inject
	private ApplicationService applicationService;
	
	@Inject
	private TwitterService twitterService;
	
	@Getter @Setter
	private List<TwitterApp> twitterApps;
	
	@Getter @Setter
	private TwitterApp twitterApp;
	
	@PostConstruct
	public void init(){
		application = Faces.getFlashAttribute("application");
		
		twitterApps = twitterService.findByApplication(application);
		
		log.info("Fetched twitter apps - {}",twitterApps);
	}
	
	
	@I18nAware
	public void save(){
		applicationService.update(application);
	}
	
	public void twitterInit(){
		log.info("New twitter account to be added.");
		
		twitterApp = new TwitterApp();
	}
	
	@I18nAware
	public void saveTwitter() {
		twitterApp.setApplication(application);
		twitterApp.setPublicKey(UUID.randomUUID().toString());
		
		twitterService.save(twitterApp);
		
		twitterApps = twitterService.findByApplication(application);
		
		log.info("Fetched twitter apps - {}",twitterApps);
	}
	
}
